{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pylab as plt\n",
    "from scipy.signal import firwin\n",
    "from utils import *\n",
    "%matplotlib inline\n",
    "np.set_printoptions(precision=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "PHASE = 8\n",
    "TAPS = 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "taps=[\n",
    " 9.3929564831e-19, -8.5710714608e-03,  7.2517798908e-02,  4.3605327255e-01,  4.3605327255e-01,  7.2517798908e-02, -8.5710714608e-03,  9.3929564831e-19, \n",
    "-9.3933518778e-05, -9.3470662763e-03,  1.0586786389e-01,  4.7373462205e-01,  3.9133956357e-01,  4.5584002395e-02, -7.0509359120e-03, -3.4116199941e-05, \n",
    "-4.3931118377e-04, -8.7269190693e-03,  1.4534313985e-01,  5.0225839591e-01,  3.4195210120e-01,  2.4904926090e-02, -5.2763056707e-03, -1.6027127365e-05, \n",
    "-1.1147853413e-03, -5.9135767933e-03,  1.9018169596e-01,  5.2005122659e-01,  2.9041486427e-01,  9.9677898880e-03, -3.5819283529e-03, -5.2862150048e-06, \n",
    "-2.1652550797e-03,  4.8535058817e-18,  2.3911664467e-01,  5.2609722081e-01,  2.3911664467e-01,  4.8535058817e-18, -2.1652550797e-03, -3.9775090540e-21, \n",
    "-3.5819034011e-03,  9.9677204519e-03,  2.9041284122e-01,  5.2004760389e-01,  1.9018037114e-01, -5.9135355990e-03, -1.1147775756e-03,  1.6798715657e-06, \n",
    "-5.2762137661e-03,  2.4904492288e-02,  3.4194614496e-01,  5.0224964740e-01,  1.4534060822e-01, -8.7267670608e-03, -4.3930353169e-04,  1.3915007357e-06, \n",
    "-7.0506919553e-03,  4.5582425225e-02,  3.9132602354e-01,  4.7371823122e-01,  1.0586420095e-01, -9.3467428754e-03, -9.3930268754e-05,  4.8416553542e-07,   \n",
    "    \n",
    "     ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# test signal\n",
    "FREQ = 5\n",
    "N1 = 32\n",
    "x = np.arange(0, 1, 1/N1)\n",
    "y = np.sin(x*2*np.pi*FREQ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# bank selection\n",
    "def c(p):\n",
    "    return [taps[x+(PHASE-1-p)*TAPS] for x in range(TAPS)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "N2 = N1*PHASE\n",
    "y_interpolated = np.zeros(N2)\n",
    "for i in range(TAPS, N1):\n",
    "    d = y[i-TAPS:i]\n",
    "    for OFFSET in range(PHASE):\n",
    "        y_interpolated[i*PHASE+OFFSET] = np.sum(d*c(OFFSET))\n",
    "        #print(OFFSET, np.sum(d*c(OFFSET)))\n",
    "# delay compensation\n",
    "fir_delay = -TAPS\n",
    "phase_offset = (OFFSET)/PHASE\n",
    "x_interpolated = np.arange(0, 1, 1/N2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_ideal = np.arange(0, 1, 1/1000)\n",
    "y_ideal = np.sin(x_ideal*2*np.pi*FREQ)\n",
    "plt.plot(x_ideal, y_ideal, label=\"ideal\")\n",
    "plt.plot(x, y, 'o', label=\"sampled\")\n",
    "plt.plot(x_interpolated, y_interpolated, '.', label=\"FIR\")\n",
    "plt.xlim(0.5, 0.7)\n",
    "plt.grid()\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print_output(y_interpolated, PHASE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "plt.plot(y_interpolated)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
